﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using FarmSmartSleep.Organ.API.Application.ProductionManagement.FemaleRen;
using MediatR;
using System.Reflection;

namespace FarmSmartSleep.Organ.API.Application.Descendantsafter
{
    public class DescendantsafterSelectHandler : IRequestHandler<DescendantsafterSelectCommand, Result>
    {
        public readonly IBaseRepository<Descendants> descendantsRepository;//后裔转种
        public readonly IBaseRepository<Cottages> cottagesRepository;//栋舍
        public readonly IBaseRepository<Fields> fieldsRepository;//栏位
        private readonly IBaseRepository<SleepInfor> sleepInforRepository;//羊只档案

        public DescendantsafterSelectHandler(IBaseRepository<Descendants> descendantsRepository, IBaseRepository<Cottages> cottagesRepository, IBaseRepository<Fields> fieldsRepository, IBaseRepository<SleepInfor> sleepInforRepository)
        {
            this.descendantsRepository = descendantsRepository;//后裔转种
            this.cottagesRepository = cottagesRepository;//栋舍
            this.fieldsRepository = fieldsRepository;//栏位
            this.sleepInforRepository = sleepInforRepository;//羊只档案
        }

        /// <summary>
        /// 后裔转种记录查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(DescendantsafterSelectCommand request, CancellationToken cancellationToken)
        {
            //throw new NotImplementedException();
            var result = new Result() { Code = (int)CodeEnum.success };
            try
            {
                var list = (from a in sleepInforRepository.GetAll()
                            join b in descendantsRepository.GetAll() on a.SleepInforId equals b.SleepInforId
                            join c in cottagesRepository.GetAll() on b.CurrentBarn equals c.CottagesId
                            join d in fieldsRepository.GetAll() on a.FieIdId equals d.FieIdId
                            select new DescendantsDto()
                            {
                                DescendantsId = b.DescendantsId,//后裔转种编号
                                DescendantsDate = b.DescendantsDate,//转种日期
                                SleepInforId = a.SleepInforId,//档案编号
                                EarNumber = a.EarNumber,//耳号
                                DescendantsCode = b.DescendantsCode,//单据号
                                DescendantsLiu = b.DescendantsLiu,//流水号
                                Gender = a.Gender,//性别
                                CurrentBarn = c.CottageName,//当前栋舍
                                CurrentBarnId = c.CottagesId,//当前栋舍编号
                                CurrentPen = d.FieIdName,//当前栏位
                                CurrentPenId = d.FieIdId,//当前栏位编号
                                TransferBarn = c.CottageName,//转入栋舍
                                TransferBarnId = c.CottagesId,//转入栋舍编号
                                TransferPen = d.FieIdName,//转入栏位
                                TransferPenId = d.FieIdId,//转入栏位编号
                                Deletes = b.Deletes,//删除标识
                                CreateId = b.CreateId,//创建人
                                CreateTime = b.CreateTime,//创建时间
                                UpdateId = b.UpdateId,//修改人
                                UpdateTime = b.UpdateTime,//修改时间
                                ExamId = b.ExamId,//审核人
                                ExamTime = b.ExamTime,//审核时间
                            }).ToList();
                //转种日期
                if (!string.IsNullOrEmpty(request.start))
                {
                    list = list.Where(x => x.DescendantsDate >= DateTime.Parse(request.start)).ToList();
                }
                if (!string.IsNullOrEmpty(request.end))
                {
                    list = list.Where(x => x.DescendantsDate < DateTime.Parse(request.end)).ToList();
                }
                //后裔耳号
                if (request.sleepInforId > 0)
                {
                    list = list.Where(x => x.SleepInforId == request.sleepInforId).ToList();
                }
                if (list != null)
                {
                    result.Code = (int)CodeEnum.success;
                    result.Data = list;
                }
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
